
import cv2
import math
import numpy as np
flag = False
x1=y1=0


##Alt+3
def screenShot(event,x,y,flags,param):
  global x1,y1,flag,img,temp
  if event==cv2.EVENT_LBUTTONDOWN: #鼠标左键按下
    flag = True #表示当前鼠标左键是按下的
    x1 = x
    y1 = y
  elif event==cv2.EVENT_MOUSEMOVE: #鼠标移动
    if(flag):
       img = temp.copy()#原图复制(把绘制的矩形清空)
       #                    左上    右下   倾斜角度  显示范围0~180，颜色,   线条宽度
       #cv2.ellipse(img,(x1,y1),(abs(x-x1),abs(y-y1)),0,      0,360,      (0,255,0),1, cv2.LINE_AA)
       #
       #矩形外接椭圆
       cv2.ellipse(img,(math.ceil((x1+x)/2),math.ceil((y1+y)/2)),(math.ceil(1.414*abs(x-x1)/2),math.ceil(1.414*abs(y-y1)/2)),0,      0,360,      (255,0,0),1, cv2.LINE_AA)
       #矩形内切椭圆
       cv2.ellipse(img,(math.ceil((x1+x)/2),math.ceil((y1+y)/2)),(math.ceil(abs(x-x1)/2),math.ceil(abs(y-y1)/2)),0,      0,360,      (0,0,255),1, cv2.LINE_AA)

       cv2.rectangle(img,(x1,y1),(x,y),(0,255,0),2)
  elif event==cv2.EVENT_LBUTTONUP: #鼠标左键弹起
      flag = False
      
      ROI = temp[y1:y,x1:x] #---y1:y2, x1:x2
      temp=img;
      #是否每次画圆后清除圆
      #img=temp.copy()
     
    
img=cv2.imread('images/a.jpg') #读取图像

temp = img.copy() #原图备份

cv2.namedWindow('screenShot')

cv2.setMouseCallback('screenShot',screenShot)

while(1):
  cv2.imshow('screenShot',img)
  if cv2.waitKey(1)&0xFF==27: #Esc按下退出
    break
cv2.destroyAllWindows()





